package com.fxu.gen;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import cn.hutool.core.util.StrUtil;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AESUtils {
    private static final Logger log = LoggerFactory.getLogger(AESUtils.class);
    private static final String KEY_ALGORITHM = "AES";
    private static final String CHAR_SET = "UTF-8";
    private static final Integer SECRET_KEY_LENGTH = 128;
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public AESUtils() {
    }

    public static String encrypt(String content, String salt) {
        if (!StrUtil.isBlank(content) && !StrUtil.isBlank(salt)) {
            try {
                Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
                byte[] byteContent = content.getBytes(CHAR_SET);
                cipher.init(1, getSecretKey(salt));
                byte[] encryptByte = cipher.doFinal(byteContent);
                return Base64.encodeBase64String(encryptByte);
            } catch (Exception var5) {
                log.error("AES加密异常，content:{},salt:{}", new Object[]{content, salt, var5});
                return null;
            }
        } else {
            log.error("AES加密参数为空，content:{},salt:{}", content, salt);
            return null;
        }
    }

    public static String decrypt(String content, String salt) {
        if (!StrUtil.isBlank(content) && !StrUtil.isBlank(salt)) {
            Cipher cipher = null;

            try {
                cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
                cipher.init(2, getSecretKey(salt));
                byte[] result = cipher.doFinal(Base64.decodeBase64(content));
                return new String(result, CHAR_SET);
            } catch (Exception var4) {
                log.error("AES解密异常，content:{},salt:{}", new Object[]{content, salt, var4});
                return null;
            }
        } else {
            log.error("AES解密参数为空，content:{},salt:{}", content, salt);
            return null;
        }
    }

    private static SecretKeySpec getSecretKey(String password) throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(password.getBytes());
        keyGenerator.init(SECRET_KEY_LENGTH, random);
        SecretKey secretKey = keyGenerator.generateKey();
        return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
    }

    public static void main(String[] args) {
        String content = "fxb9ijgCRxsEpgIG9wFbbTAJtv6XaOYOOxuA36M0gXoz/Lg5UzGBXQ8+2+drndGtmNCskcPveue3+DqWzMeMe9LOTu+dz+3AGdGSlkWu1r4=";
        String salt = "2R*7dpJ%+%?ExfGa";
        //解密
        System.out.println(decrypt(content, salt));
    }
}